import toolkit from './toolkit.js';
export default {
	check(matrix){
		this._matrix = matrix;
		this._matrixMarks = toolkit.makeMatrix(true)
		this.checkRows()
		this.checkCols()
		this.checkBoxes()
		//every 检查数组每个元素
		
		this._success = this._matrixMarks.every(row=>row.every(mark=>mark)) ;
		if(!this._success){
			return this._matrixMarks;
		}else{
			return toolkit.makeMatrix(true);
		}
	},
	checkRows(){
		for(let rowIndex=0;rowIndex<9;rowIndex++){
			const row = this._matrix[rowIndex];
			const marks = this.checkArray(row);
			for(let colIndex=0;colIndex<9;colIndex++){
				if(!marks[colIndex]){
					this._matrixMarks[rowIndex][colIndex] = false;
				}
			}
		}
	},
	checkCols(){
		for(let colIndex=0;colIndex<9;colIndex++){
			const col = this._matrix.map( i=>i[colIndex])
			const marks = this.checkArray(col);
			for(let rowIndex=0;rowIndex<9;rowIndex++){
				if(!marks[rowIndex]){
					this._matrixMarks[rowIndex][colIndex] = false;
				}
			}
			
		}
	},
	checkBoxes(){
		for(let boxIndex=0;boxIndex<9;boxIndex++){
			const box = toolkit.boxToolkit.getBoxCells(this._matrix,boxIndex)
			const marks = this.checkArray(box);
			for(let cellIndex=0;cellIndex<9;cellIndex++){				
				if(!marks[cellIndex]){
					const {rowIndex,colIndex} = toolkit.boxToolkit.converFromBoxIndex(boxIndex,cellIndex)
					this._matrixMarks[rowIndex][colIndex] = false;
				}
				
				
			}
			
		}
	},
	checkArray(array){
		const length = array.length;
		const  marks = new Array(length)
		marks.fill(true);
		for(let i=-0;i<length-1;i++){
			if(!marks[i]){
				continue;
			}
			const v = array[i]
			//是否有效
			if(!v){
				marks[i] = false;
				continue;
			}
			//是否重复
			for(let n=i+1;n<length;n++){
				if(v==array[n]){
					marks[i] = false;
					marks[n] = false;
				}
			}
		}
		return marks;
	}
}
